package org.springframework.android.showcase.location;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.Toast;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.OverlayItem;

public class OverItemLay extends ItemizedOverlay<OverlayItem> {

    private List<OverlayItem> geoList = new ArrayList<OverlayItem>();

    private Context mContext;

    public OverItemLay(Drawable arg0) {
        super(arg0);
    }

    public OverItemLay(Drawable marker, Context context) {
        super(boundCenterBottom(marker));

        this.mContext = context;

        populate(); // createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
    }

    public OverItemLay(Drawable marker, Context context, List<Location> locations) {
        super(boundCenterBottom(marker));

        this.mContext = context;

        if (locations != null && locations.size() > 0) {
            for (int i = 0; i < locations.size(); i++) {
                Location loc = locations.get(i);
                String lat = loc.getLat();
                String lon = loc.getLon();
                if (!TextUtils.isEmpty(lat) && !TextUtils.isEmpty(lon)) {

                    GeoPoint p = new GeoPoint((int) (Double.parseDouble(lat) * 1E6),
                            (int) (Double.parseDouble(lon) * 1E6));
                    geoList.add(new OverlayItem(p, "P" + i, "电话：" + loc.getPhoneNumber() + " 位置：" + loc.getAddress()));
                }
            }
        }

        populate(); // createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
    }

    @Override
    protected OverlayItem createItem(int i) {
        return geoList.get(i);
    }

    @Override
    public int size() {
        return geoList.size();
    }

    @Override
    // 处理当点击事件
    protected boolean onTap(int i) {
        Toast.makeText(this.mContext, geoList.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
        return true;
    }
}
